package wt.hpad.activity.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import wt.hpad.ConstantFields;
import wt.hpad.R;
import wt.hpad.activity.cart.CartActivity;
import wt.hpad.model.adapter.TypeTabAdapter;
import wt.hpad.model.bean.DishType;
import wt.hpad.utils.AppUtil;

/**
 * 点菜 activity
 */
public class OrderActivity extends AppCompatActivity implements OrderContract.View {

    @BindView(R.id.tab_dishtype)
    VerticalTabLayout tabDishtype;
    @BindView(R.id.order_content)
    FrameLayout orderContent;
    @BindView(R.id.img_cart)
    ImageView imgCart;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.img_enter_cart)
    ImageView imgEnterCart;
    @BindView(R.id.rl_order)
    RelativeLayout rlOrder;

    OrderContract.Presenter mOrderPresenter;

    List<Fragment> mFragments;
    private Unbinder bind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_act);

        initUI();
        initData();
    }

    private void initData() {
        tvOrderTitle.setText(ConstantFields.CURRENT_TABLE);
        initFragments();
        if (mFragments.size() != 0) {
            tabDishtype.setupWithFragment(getSupportFragmentManager(),
                    R.id.order_content,
                    mFragments,
                    new TypeTabAdapter<DishType>(mOrderPresenter.getDishTypes()));
        }
        ConstantFields.ORDERVIEW_PARENTVIEWGROUP = rlOrder;
        ConstantFields.ORDERVIEW_ENDVIEW = imgCart;
        ConstantFields.ORDERVIEW_CART_NUM = tvOrderNum;
        if (ConstantFields.CART_CURRENT == null) {
            ConstantFields.CART_CURRENT = new ArrayList<>();
        } else {
            tvOrderNum.setText(String.valueOf(ConstantFields.CART_CURRENT.size()));
        }
    }

    private void initFragments() {
        mFragments = mOrderPresenter.initTypeFragments();
    }

    private void initUI() {
        AppUtil.hideBarAndTransparentTime(this);
        AppUtil.hideBottomUIMenuAll(this);
        bind = ButterKnife.bind(this);
        setPresenter(new OrderPresenter(this));
    }

    @OnClick(R.id.img_enter_cart)
    public void onViewClicked() {
        startActivity(new Intent(this, CartActivity.class));
        this.finish();
    }


    @Override
    public void setPresenter(OrderContract.Presenter presenter) {
        mOrderPresenter = presenter;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFragments.clear();
        ConstantFields.cleanCacheView();
        bind.unbind();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ConstantFields.cleanCacheView();
        ConstantFields.cleanCacheData();
    }

    @OnClick(R.id.tv_order_title)
    public void onTilteClicked() {
        ConstantFields.cleanCacheView();
        ConstantFields.cleanCacheData();
        this.finish();
    }
}
